package com.kbt.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.*;
import com.kbt.adapter.AlbumGridViewAdapter;
import com.kbt.base.BaseApplication;
import com.kbt.util.data.StaticData;
import com.kbt.util.photo.*;


public class AlbumActivity extends Activity {
    private GridView gridView;
    private TextView tv;
    private AlbumGridViewAdapter gridImageAdapter;
    private Button okButton;
    private Button back;
    private Button cancel;
    private Intent intent;
    private Context mContext;
    private ArrayList<ImageItem> dataList;
    private AlbumHelper helper;
    public static List<ImageBucket> contentList;
    public static Bitmap bitmap;
    private Bundle bl;
    private String state;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.plugin_camera_album);
        BaseApplication.getInstance().addActivity(AlbumActivity.this);

        this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_return_btn);

        //设置返回按钮事件,关闭当前Activity，返回数据
        ImageView returnImg = (ImageView) findViewById(R.id.return_btn);
        returnImg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AlbumActivity.this,AfterServiceActivity.class);
                intent.putExtra("bundle", bl);
                intent.putExtra(StaticData.ORDER_TYPE, state);
                startActivity(intent);
                BaseApplication.getInstance().removeActivity(AlbumActivity.this);
                AlbumActivity.this.finish();
            }
        });

        TextView appTitleView = (TextView) findViewById(R.id.center_title);
        appTitleView.setText("选择照片");

        PublicWay.activityList.add(this);
        mContext = this;
        init();
        initListener();
        isShowOkBt();
    }



    private class AlbumSendListener implements OnClickListener {
        public void onClick(View v) {
            overridePendingTransition(R.anim.activity_translate_in, R.anim.activity_translate_out);
            intent.setClass(mContext, AfterServiceActivity.class);
            intent.putExtra("bundle", bl);
            intent.putExtra(StaticData.ORDER_TYPE, state);
            startActivity(intent);
            BaseApplication.getInstance().removeActivity(AlbumActivity.this);
            AlbumActivity.this.finish();
        }

    }


    private class CancelListener implements OnClickListener {
        public void onClick(View v) {
            intent.setClass(mContext, AfterServiceActivity.class);
            intent.putExtra("bundle", bl);
            intent.putExtra(StaticData.ORDER_TYPE, state);
            startActivity(intent);
            Bimp.tempSelectBitmap.clear();
            Bimp.max = 0;
            BaseApplication.getInstance().removeActivity(AlbumActivity.this);
            AlbumActivity.this.finish();
        }
    }


    private void init() {

        if(!hasSDCard()){
            Toast.makeText(AlbumActivity.this,"请检查sd卡是否存在!",Toast.LENGTH_SHORT).show();
           return;
        }
        helper = AlbumHelper.getHelper();
        helper.init(getApplicationContext());

        contentList = helper.getImagesBucketList(false);
        dataList = new ArrayList<ImageItem>();
        for (int i = 0; i < contentList.size(); i++) {
            Log.e("contentList",contentList.get(i).imageList+"");
            dataList.addAll(contentList.get(i).imageList);
        }
        cancel = (Button) findViewById(R.id.cancel);
        cancel.setOnClickListener(new CancelListener());
        intent = getIntent();
        bl = intent.getBundleExtra("bundle");
        state = intent.getStringExtra("state");
        gridView = (GridView) findViewById(R.id.myGrid);
        gridImageAdapter = new AlbumGridViewAdapter(this, dataList,
                Bimp.tempSelectBitmap);
        gridView.setAdapter(gridImageAdapter);
//         完成按钮
        okButton = (Button) findViewById(R.id.ok_button);
        okButton.setText("完成" + "(" + Bimp.tempSelectBitmap.size()
                + "/" + PublicWay.num + ")");
    }
    //判断sdk是否存在
    public static boolean hasSDCard() {
        return Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState());
    }
    private void initListener() {

        gridImageAdapter
                .setOnItemClickListener(new AlbumGridViewAdapter.OnItemClickListener() {

                    @SuppressLint("ShowToast")
                    @Override
                    public void onItemClick(final ToggleButton toggleButton,
                                            int position, boolean isChecked, Button chooseBt) {
                        if (Bimp.tempSelectBitmap.size() >= PublicWay.num) {
                            toggleButton.setChecked(false);
                            chooseBt.setVisibility(View.GONE);
                            if (!removeOneData(dataList.get(position))) {
                                Toast.makeText(AlbumActivity.this, "最多选择4张哦!",
                                        Toast.LENGTH_SHORT).show();
                            }
                            return;
                        }
                        if (isChecked) {
                            chooseBt.setVisibility(View.VISIBLE);
                            Bimp.tempSelectBitmap.add(dataList.get(position));
                            okButton.setText("完成" + "(" + Bimp.tempSelectBitmap.size()
                                    + "/" + PublicWay.num + ")");
                        } else {
                            Bimp.tempSelectBitmap.remove(dataList.get(position));
                            chooseBt.setVisibility(View.GONE);
                            okButton.setText("完成" + "(" + Bimp.tempSelectBitmap.size() + "/" + PublicWay.num + ")");
                        }
                        isShowOkBt();
                    }
                });

        okButton.setOnClickListener(new AlbumSendListener());
    }

    private boolean removeOneData(ImageItem imageItem) {
        if (Bimp.tempSelectBitmap.contains(imageItem)) {
            Bimp.tempSelectBitmap.remove(imageItem);
            okButton.setText("完成" + "(" + Bimp.tempSelectBitmap.size() + "/" + PublicWay.num + ")");
            return true;
        }
        return false;
    }

    public void isShowOkBt() {
        if (Bimp.tempSelectBitmap.size() > 0) {
            okButton.setText("完成" + "(" + Bimp.tempSelectBitmap.size() + "/" + PublicWay.num + ")");
            okButton.setPressed(true);
            okButton.setClickable(true);
            okButton.setTextColor(Color.WHITE);
        } else {
            okButton.setText("完成" + "(" + Bimp.tempSelectBitmap.size() + "/" + PublicWay.num + ")");
            okButton.setPressed(false);
            okButton.setClickable(false);
            okButton.setTextColor(Color.parseColor("#E1E0DE"));
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            intent.setClass(mContext, AfterServiceActivity.class);
            intent.putExtra("bundle", bl);
            intent.putExtra(StaticData.ORDER_TYPE, state);
            startActivity(intent);
            BaseApplication.getInstance().removeActivity(AlbumActivity.this);
            AlbumActivity.this.finish();
        }
        return false;

    }

    @Override
    protected void onRestart() {
        isShowOkBt();
        super.onRestart();
    }
}
